package com.wsdcd.zlg.sb.model.service.impl;

import com.wsdcd.zlg.sb.core.exception.ERPException;
import com.wsdcd.zlg.sb.mapper.CustomerMapper;
import com.wsdcd.zlg.sb.model.entity.Customer;
import com.wsdcd.zlg.sb.model.service.CustomerService;
import com.wsdcd.zlg.sb.util.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class CustomerServiceImpl implements CustomerService {
	private final CustomerMapper customerMapper;
	
	@Override
	public List<Customer> select() {
		return customerMapper.select();
	}

	@Override
	public PagerUtil<Customer> selectByPage(PageParameter pageParameter){
		int count = customerMapper.getCount();
		List<Customer> data;
		data = customerMapper.getPageList(pageParameter);
		return new PagerUtil<>(count, data);
	}

	@Override
	public void insert(Customer customer) throws ERPException {
		String customerName = customer.getCustomerName();
		if(customerName == null || customerName.trim().equals("")){
			throw new ERPException("输入的客户名称不能为空");
		}
		String customerFullName = customer.getCustomerFullName();
		if(customerFullName == null || customerFullName.trim().equals("")){
			throw new ERPException("输入的客户全称不能为空");
		}
		Customer oldCustomer = customerMapper.selectByCustomerFullName(customerFullName);
		if(oldCustomer != null){
			throw new ERPException("输入的客户全称已存在");
		}
		String address = customer.getAddress();
		if(address == null || address.trim().equals("")){
			throw new ERPException("输入的地址不能为空");
		}
		String faxString = customer.getFax();
		if(!StringIsNumber.isInt(faxString)){
			throw new ERPException("输入的传真必须是数字");
		}
		String emailString = customer.getEmail();
		if(!StringIsEmail.simpleEmail(emailString)){
			throw new ERPException("输入的邮箱格式不正确");
		}
		String managerNameString = customer.getManagerName();
		if(!StringIsSinogram.isSinogram(managerNameString)){
			throw new ERPException("经理名称必须是中文名");
		}
		String telephoneString = customer.getTelephone();
		if(!StringIsTelephone.isTelephone(telephoneString)){
			throw new ERPException("输入的手机号格式不正确");
		}
		
		customerMapper.insert(customer);
	}

	@Override
	public Customer selectByCustomerId(String customerId) {
		return customerMapper.selectByCustomerId(customerId);
	}

	@Override
	public void update(Customer customer) throws ERPException {
		int customerId = customer.getCustomerId();
		String customerIdString = customerId+"";
		Customer oldCustomer = customerMapper.selectByCustomerId(customerIdString);
		if(oldCustomer==null){
			throw new ERPException("修改失败，想要修改的客户编号不存在");
		}else if(oldCustomer.equals(customer)){
			throw new ERPException("修改失败，修改后的数据不能和之前相同");
		}
		
		Customer oldCustomer2 = customerMapper.selectByCustomerFullName(customer.getCustomerFullName());
		if( oldCustomer2 != null && oldCustomer2.getCustomerId()!=customerId ){
			throw new ERPException("修改失败，想要修改的客户全称已被占用");
		}
		customerMapper.update(customer);
	}

	@Override
	public void deleteAll(int[] customerId) {
		customerMapper.deleteAll(customerId);
	}

}
